package com.swufe.lianxi;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button btn_count;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onClick(View view) {
        EditText editHeight = (EditText)findViewById(R.id.height);
        EditText editWeidth = (EditText)findViewById(R.id.weight);
        TextView txtResault = (TextView)findViewById(R.id.txt);
        TextView BMIText = (TextView)findViewById(R.id.Text2);
        btn_count=(Button)findViewById(R.id.btn_count);
        //保留两位小数点
        DecimalFormat df = new DecimalFormat("0.00");
        //数据转化
        Double height = Double.parseDouble(editHeight.getText().toString());
        Double weight = Double.parseDouble(editWeidth.getText().toString());
        Double bim;
        bim = weight / (height*height);
        BMIText.setText(df.format(bim));
        //判断语句
        if(bim<18.5){
            txtResault.setText("BMI: "+bim.toString()+"  您的体重偏轻");
        }
        else  if(bim<=23.9){
            txtResault.setText("BMI: "+bim.toString()+"  您的体重正常");
        }
        else if(bim<=27.9){
            txtResault.setText("BMI: "+bim.toString()+"  您的体重偏重");
        }
        else {
            txtResault.setText("BMI: "+bim.toString()+"  您的体重肥胖");
        }
        editWeidth.setText("");
        editHeight.setText("");
        BMIText.setText("");
    }
    }


